package com.primus.server.web.controller;

import com.primus.server.entity.store.Product;
import com.primus.server.service.store.ProductService;
import com.primus.server.web.ResponseBase;
import com.primus.server.web.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("product")
public class ProductController extends BaseController {

    private final ProductService productService;

    @Autowired
    ProductController(ProductService productService) {
        this.productService = productService;
    }

    @DeleteMapping(path = "/{id}")
    public ResponseBase<Void> deleteProduct(@PathVariable Long id) {
        this.productService.deleteById(id);
        return this.success();
    }

    @PostMapping(path = "/upsert")
    public ResponseBase<Void> updateProduct(@RequestBody ProductVO pvo) {
        Product product = this.transfer(pvo, new Product());
        this.productService.save(product);
        return this.success();
    }
}
